package com.neulogistics.contrib.usercenter.domain.model.enumeration;

import com.neulogistics.contrib.usercenter.domain.exception.UserCenterDomainException;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.ddd.domain.seedwork.Enumeration;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class UserGender extends Enumeration {

    /**
     * 用户性别: 0:男；1:女
     */
    public static final int Male_Id = 0;
    public static final int Female_Id = 1;

    public static final UserGender Male = new UserGender(Male_Id, "Male");
    public static final UserGender Female = new UserGender(Female_Id, "Female");

    private static final List<UserGender> list = Arrays.asList(
            Male,
            Female
    );

    protected UserGender(int id, String name) {
        super(id, name);
    }

    public static List<UserGender> getList() {
        return list;
    }

    public static UserGender fromName(String name) throws UserCenterDomainException {
        Optional<UserGender> optional = list.stream().filter(p -> p.name.equals(name)).findFirst();

        if (optional.isPresent()) {
            return optional.get();
        } else {
            throw new UserCenterDomainException(
                    ErrorCode.OPERATION_ERROR,
                    String.format("Possible values for {%s}: %s",
                            UserGender.class.getSimpleName(),
                            list.stream()
                                    .map(p -> p.name)
                                    .collect(Collectors.joining(",")))
            );
        }
    }

    @Override
    public int compareTo(Object o) {
        return 0;
    }
}
